<?php

/**
 * KPI管理
 * @desc
 */
class Api_KPI extends PhalApi_Api {

    public function getRules() {
        $departmentList = DI()->DingTalk->departmentList();
        $rules = [
            '*'      => [
                'token'      => ['name' => 'token', 'type' => 'string', 'min' => 12, 'max' => 12, 'require' => true, 'desc' => 'tokenID'],
            ],
            'add'    => [
                'pname'                  => ['name' => 'pname', 'type' => 'string', 'require' => true, 'desc' => '项目名称'],
                'PM'                     => ['name' => 'PM', 'type' => 'string', 'require' => true, 'desc' => '项目经理 userid'],
                'contract_no'            => ['name' => 'contract_no', 'type' => 'string', 'require' => true, 'desc' => '合同编号'],
                'contract_val'           => ['name' => 'contract_val', 'type' => 'int', 'require' => true, 'desc' => '合同价格'],
                'contract_date'          => ['name' => 'contract_date', 'type' => 'date', 'require' => true, 'desc' => '合同签订时间'],
                'contractCollectionDate' => ['name' => 'contractCollectionDate', 'type' => 'date', 'require' => true, 'desc' => '合同收款时间'],
                'ProStartDate'           => ['name' => 'ProStartDate', 'type' => 'date', 'require' => true, 'desc' => '项目启动时间'],
                'ExpectedCollectionDate' => ['name' => 'ExpectedCollectionDate', 'type' => 'date', 'require' => true, 'desc' => '预期收款时间'],
                'staff1'                 => ['name' => 'staff1', 'type' => 'string', 'require' => true, 'desc' => '一级负责人员 userid'],
                'staff2'                 => ['name' => 'staff2', 'type' => 'string', 'require' => true, 'desc' => '二级参与人员 userid'],
                'staff3'                 => ['name' => 'staff3', 'type' => 'string', 'require' => true, 'desc' => '三级其他人员 userid'],
                'ProFinishDate'          => ['name' => 'ProFinishDate', 'type' => 'date', 'require' => false, 'desc' => '项目完成时间'],
                'collectionDate'         => ['name' => 'collectionDate', 'type' => 'date', 'require' => false, 'desc' => '收款时间'],

            ],
            'edit'   => [
                'pname'                  => ['name' => 'pname', 'type' => 'string', 'require' => true, 'desc' => '项目名称'],
                'PM'                     => ['name' => 'PM', 'type' => 'string', 'require' => true, 'desc' => '项目经理'],
                'contract_no'            => ['name' => 'contract_no', 'type' => 'string', 'require' => true, 'desc' => '合同编号'],
                'contract_val'           => ['name' => 'contract_val', 'type' => 'int', 'require' => true, 'desc' => '合同价格'],
                'contract_date'          => ['name' => 'contract_date', 'type' => 'date', 'require' => true, 'desc' => '合同签订时间'],
                'contractCollectionDate' => ['name' => 'contractCollectionDate', 'type' => 'date', 'require' => true, 'desc' => '合同收款时间'],
                'ProStartDate'           => ['name' => 'ProStartDate', 'type' => 'date', 'require' => true, 'desc' => '项目启动时间'],
                'ExpectedCollectionDate' => ['name' => 'ExpectedCollectionDate', 'type' => 'date', 'require' => true, 'desc' => '预期收款时间'],
                'staff1'                 => ['name' => 'staff1', 'type' => 'string', 'require' => true, 'desc' => '一级负责人员'],
                'staff2'                 => ['name' => 'staff2', 'type' => 'string', 'require' => true, 'desc' => '二级参与人员'],
                'staff3'                 => ['name' => 'staff3', 'type' => 'string', 'require' => true, 'desc' => '三级其他人员'],
                'ProFinishDate'          => ['name' => 'ProFinishDate', 'type' => 'date', 'require' => false, 'desc' => '项目完成时间'],
                'collectionDate'         => ['name' => 'collectionDate', 'type' => 'date', 'require' => false, 'desc' => '收款时间'],
            ],
            'lists'  => [
                'keyword' => ['name' => "keyword", 'type' => 'string', 'require' => false, 'desc' => '关键词'],
                'page'    => ['name' => "page", 'type' => 'int', 'default' => 1, 'min' => 1, 'require' => false, 'desc' => '当前第几页'],
                'pageMax' => ['name' => "pageMax", 'type' => 'int', 'default' => 10, 'min' => 1, 'max' => 100, 'require' => false, 'desc' => '每页多少条']
            ],
            'ratioSet'  => [
                'staff1' => ['name' => 'staff1', 'type' => 'int', 'require' => true, 'desc' => '一级负责人员分成比例'],
                'staff2' => ['name' => 'staff2', 'type' => 'int', 'require' => true, 'desc' => '二级参与人员分成比例'],
                'staff3' => ['name' => 'staff3', 'type' => 'int', 'require' => true, 'desc' => '三级其他人员分成比例'],

            ],
            'delete' => [
                'id' => ['name' => 'id', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => 'KPI id'],
            ],
            'info'   => [
                'id' => ['name' => 'id', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => 'KPI id'],
            ]
        ];

        foreach ($departmentList as $dk => $di) {
            if ($di['id'] > 1) {
                $r = ['type' => 'int', 'require' => true];
                $r['name'] = 'p' . $di['id'];
                $r['desc'] = $di['name'] . '分成比例';
                $rules['ratioSet']['p' . $di['id']] = $r;
            }
        }

        return $rules;
    }

    /**
     * KPI 分成比例设置 todo
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return int code
     * @return object data 数据
     * @return string msg 提示信息
     */
    public function ratioSet() {

    }
    /**
     * KPI 获取分成比例 todo
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return int code
     * @return object data 数据
     * @return string msg 提示信息
     */
    public function ratio() {

    }

    /**
     * KPI添加
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return int code
     * @return object data 添加后的数据
     * @return string msg 提示信息
     */
    public function add() {

        $db = new Domain_KPI();
        $isadded = $db->isadded($this->contract_no);

        if ($isadded) {
            throw new PhalApi_Exception("该合同编号已经添加过了", 40001);
        }
        if (count($this->staff1) == 0) {
            throw new PhalApi_Exception("一级负责人员不能为空", 40002);
        }
        if (count($this->staff2) == 0) {
            throw new PhalApi_Exception("二级参与人员不能为空", 40003);
        }
        if (count($this->staff3) == 0) {
            throw new PhalApi_Exception("三级其他人员不能为空", 40004);
        }

        $data = [
            "pname"                  => $this->pname,
            "PM"                     => $this->PM,
            "contract_no"            => $this->contract_no,
            "contract_val"           => $this->contract_val,
            "contract_date"          => $this->contract_date,
            "contractCollectionDate" => $this->contractCollectionDate,
            "ProStartDate"           => $this->ProStartDate,
            "ExpectedCollectionDate" => $this->ExpectedCollectionDate,
            "staff1"                 => $this->staff1,
            "staff2"                 => $this->staff2,
            "staff3"                 => $this->staff3,
            "ProFinishDate"          => $this->ProFinishDate,
            "collectionDate"         => $this->collectionDate,
        ];


        return $db->add($data);

    }

    /**
     * 更新KPI资料 todo
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return int code
     * @return boolean data.isUpdated 是否更新成功
     * @return string msg 提示信息
     */
    public function edit() {


//        $db = new Domain_KPI();
//        $hasid = $db->hasid($this->id);
//
//        if (!$hasid) {
//            throw new PhalApi_Exception("错误的id", 40002);
//        }
//        $data = [
//            "name"        => $this->name,
//            "organize"    => $this->organize,
//            "sort"        => $this->sort,
//            "url"         => $this->url,
//            "icon_url"    => $this->icon,
//            "online_date" => $this->onlinedate,
//            "isshow"      => $this->isindex ? 1 : 0,
//            "ispublic"    => $this->ispublic ? 1 : 0,
//        ];
//
//        return [
//            "isUpdated" => $db->edit($data, $this->id)
//        ];

    }

    /**
     * KPI资料
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return int id KPIid
     * @return string msg 提示信息
     */

    public function info() {
        $db = new Domain_KPI();
        return $db->info($this->id);
    }

    /**
     * KPI删除
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return boolean data.isdelete 是否删除成功
     * @return string msg 提示信息
     */
    public function delete() {
        $db = new Domain_KPI();
        return [
            "isdelete" => $db->delete($this->id)
        ];
    }


    /**
     * KPI列表 todo
     * @desc 返回数据参见KPI资料接口
     * @return int ret HTTP状态码,200成功,其他失败
     * @return array lists 数据列表
     * @return int total 数据总数
     * @return string msg 提示信息
     */
    public function lists() {
//        $db = new Domain_KPI();
//
//        $rule = [];
//        if ($this->keyword) $rule['key'] = $this->keyword;
//        if ($this->isindex) $rule['isshow'] = $this->isindex;
//        if ($this->ispublic) $rule['ispublic'] = $this->ispublic;
//        $page = [
//            "page"    => $this->page,
//            "pageMax" => $this->pageMax,
//            "orderby" => $this->orderby,
//        ];
//
//        return $db->getlist($rule, $page);
    }

}
